package com.lupus.persistence;
import java.sql.*;
import java.util.*;

import com.lupus.common.*;
import com.lupus.persistence.sql.SqlGenerator;

public class QueryPersistence extends Persistence{
	
	public List<Map> operate(Map<String,Object> actionMap,Connection connection)throws SQLException{
		ResultSet rs = null;
		Statement statemnet = null;
		String sql = SqlGenerator.getSqlGenerator().createSql(actionMap);
		List<Map> dataList = new ArrayList<Map>();
		try{
			statemnet = connection.createStatement();
			rs = statemnet.executeQuery(sql);
			if (rs!=null){
				int colCount = rs.getMetaData().getColumnCount();
				Map<String,String> dataMap = null;
				ResultSetMetaData md = rs.getMetaData();
				while(rs.next()){
					dataMap = new HashMap<String,String>();
					for(int i=1;i<=colCount;i++){
						String fieldName = md.getColumnName(i).toUpperCase();
						String fieldValue = rs.getString(i);
						dataMap.put(fieldName,fieldValue);
						if(fieldValue!=null&&fieldValue.indexOf(".")>-1){
							String translateValue = ResourceFactory.getLocalString(fieldValue, actionMap);
							if(fieldName.endsWith("TEXT")){
								dataMap.put(fieldName,translateValue);
							}else{
								dataMap.put(fieldName+"TEXT",translateValue);
							}
						}
					}
					dataList.add(dataMap);
				}
			}
			return dataList;
		}finally{
			if(statemnet!=null){
				statemnet.close();
			}
			if(rs!=null){
				rs.close();
			}
		}
	}
}
